package com.huifaapp.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.WaitPayActivity;
import com.huifaapp.mall.bean.PendingPayBean;

import java.util.List;


/**
 * Created by mac on 2017/12/8.
 */

public class PendingPayAdapter extends RecyclerView.Adapter<PendingPayAdapter.MyViewHolder> {
    private Context context;
    private List<PendingPayBean.OrderListBean> list;

    public PendingPayAdapter(Context context, List<PendingPayBean.OrderListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.waitpay_item, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final PendingPayBean.OrderListBean orderListBean = list.get(position);   //获取当前条目的数据
        List<PendingPayBean.OrderListBean.DataBean> data = orderListBean.getData();   //获取到内层的数据集合
        PendpayAdaptertwo pendpayAdaptertwo = new PendpayAdaptertwo(context, data);   //创建内层的适配器




        pendpayAdaptertwo.setOnItemClickListener(new PendpayAdaptertwo.OnItemClickListenertwo() {
            @Override
            public void OnItem(View view, int position) {
                Intent intent = new Intent(context, WaitPayActivity.class);
                intent.putExtra("orderNo",orderListBean.getOrderNo());
                context.startActivity(intent);
            }
        });


        holder.recy111.setAdapter(pendpayAdaptertwo);   //绑定内层的适配器
        holder.sum_count.setText("共计" + orderListBean.getTotalcount() + "件商品");   //商品的总个数
        holder.sum_money.setText("合计: ¥" + orderListBean.getTotalprice());   //商品的总价
        holder.yunfei.setText("(含运费¥ " + orderListBean.getExpressFee() + ")");   //商品的运费
        holder.order_num.setText("订单编号: " + orderListBean.getOrderNo() + ""); // 订单编号

        if (onItemClickListener != null) {
            //联系买家
            holder.contactSeller.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onContactSeller(position);
                }
            });

            //取消订单
            holder.cancelOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onCancelOrder(position);
                }
            });

            //付款
            holder.payment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onPayment(position);
                }
            });




        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        RecyclerView recy111;
        TextView yunfei, sum_money, sum_count,order_num;
        Button contactSeller, cancelOrder, payment;


        public MyViewHolder(View itemView) {
            super(itemView);
            recy111 = (RecyclerView) itemView.findViewById(R.id.recy111);
            recy111.setLayoutManager(new LinearLayoutManager(context));

            yunfei = (TextView) itemView.findViewById(R.id.yunfei);
            sum_money = (TextView) itemView.findViewById(R.id.sum_money);
            sum_count = (TextView) itemView.findViewById(R.id.sum_count);
            order_num = (TextView) itemView.findViewById(R.id.waitpay_orderNumText);

            contactSeller = (Button) itemView.findViewById(R.id.contactSeller);
            cancelOrder = (Button) itemView.findViewById(R.id.cancelOrder);
            payment = (Button) itemView.findViewById(R.id.payment);

//           item.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(context, WaitPayActivity.class);
//                    context.startActivity(intent);
//                }
//            });
        }
    }

    //监听事件接口
    public interface OnItemClickListener {
        //联系买家
        void onContactSeller(int position);

        //取消订单
        void onCancelOrder(int position);

        //付款
        void onPayment(int position);


    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}